package com.huike.clues.utils;

import java.util.Calendar;
import java.util.Date;

import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.TbRulePool;
import com.huike.clues.domain.dto.TbRulePoolDTO;
import com.huike.clues.service.TbRulePoolService;
import com.huike.common.utils.spring.SpringUtils;

import javax.annotation.Resource;

/**
 * 获取结束时间的工具类
 * @author 86150
 *
 */
public class HuiKeCrmDateUtils {
	
	public static Date getDate(int time, String type, Date now){
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        if(TbRulePoolDTO.LimitTimeType.HOUR.getValue().equals(type)){
            cal.add(Calendar.HOUR, time);
        }else if(TbRulePoolDTO.LimitTimeType.DAY.getValue().equals(type)){
            cal.add(Calendar.DATE, time);
        }else if(TbRulePoolDTO.LimitTimeType.WEEK.getValue().equals(type)){
            cal.add(Calendar.DAY_OF_WEEK, time);
        }
        return cal.getTime();
    }

    //根据规则获取失效日期
    public static Date getEndDateByRule(TbAssignRecord tbAssignRecord) {
        // 静态方法无法通过DI获取bean,用字节码获取
        TbRulePoolService rulePoolService = SpringUtils.getBean(TbRulePoolService.class);
        //根据类型获取规则池对象
        TbRulePool rulePool= rulePoolService.selectTbRulePoolByType(tbAssignRecord.getType());
        if(rulePool==null){
            return null;
        }
        //获取回收时间
        Date recoveryDate=getDate(rulePool.getLimitTime().intValue(),
                rulePool.getLimitTimeType(), tbAssignRecord.getCreateTime());
        return recoveryDate;
    }
}
